package com.midea.iot.course1.javaSE4;

/**
 * @author xingwuxw
 * 类继承Thread方式，不适合资源共享
 */
public class TicketThread2 extends Thread{

    public static void main(String[] args) {

        TicketThread2 ticketLine1 = new TicketThread2();
        ticketLine1.setName("票一");
        ticketLine1.start();
        TicketThread2 ticketLine2 = new TicketThread2();
        ticketLine2.setName("票二");
        ticketLine2.start();
        TicketThread2 ticketLine3 = new TicketThread2();
        ticketLine3.setName("票三");
        ticketLine3.start();
    }
    public void run() {
        saleTicket();
    }
    private static synchronized void saleTicket() {
        int ticket = 100;
        while(true) {
            if(ticket>0) {
                try {
                    Thread.sleep(10);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(Thread.currentThread().getName()+":"+ticket);
                ticket--;
            }else{
                break;
            }
        }
    }
}
